package com.lenovo.smarttraffic.database.dao;

import android.content.Context;

/**
 * author  张猛
 **/
public class DaoManager_Collection {
    private volatile static DaoManager_Collection mInstance_Collection;
    private Context mContext;
    private static final String DB_NAME = "Collection.db";
    private static DaoMaster.DevOpenHelper mHelper_collection;
    private static DaoMaster mDaoMaster_collection;
    private static DaoSession mDaoSession_collection;

    public DaoManager_Collection() {
    }

    public static DaoManager_Collection getInstance_Collection() {
        if (mInstance_Collection == null) {
            synchronized (DaoManager_Collection.class) {
                if (mInstance_Collection == null) {
                    mInstance_Collection = new DaoManager_Collection();
                }
            }
        }
        return mInstance_Collection;
    }

    public void init(Context context) {
        this.mContext = context;
    }


    public DaoMaster getDaoMaster_Collection() {
        if (mDaoMaster_collection == null) {
            mHelper_collection = new DaoMaster.DevOpenHelper(mContext, DB_NAME, null);
            mDaoMaster_collection = new DaoMaster(mHelper_collection.getWritableDatabase());
        }
        return mDaoMaster_collection;
    }

    public DaoSession getDaoSession_Collection() {

        if (mDaoSession_collection == null) {
            if (mDaoMaster_collection == null) {
                mDaoMaster_collection = getDaoMaster_Collection();
            }
            mDaoSession_collection = mDaoMaster_collection.newSession();
        }
        return mDaoSession_collection;
    }

    public void closeConnection() {
        closeHelper();
        closerDaoSession();
    }

    private void closerDaoSession() {
        if (mDaoSession_collection != null) {
            mDaoSession_collection.clear();
            mDaoSession_collection = null;
        }
    }

    private void closeHelper() {
        if (mHelper_collection != null) {
            mHelper_collection.close();
            mHelper_collection = null;
        }
    }
}
